﻿using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoL.Core.Units;
using LBoL.EntityLib.EnemyUnits.Normal;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards;

namespace ProbeMod.StatusEffects
{

    #region 反脆弱
    /// <summary>
    /// '{PlayerName}通过打出牌获得|格挡|和|护盾|提高 {Value}%，剩余 {Duration} 回合。
    /// </summary>
    public sealed class ProbeReversalFragilSeDef : ProbeStatusEffectTemplate
    {
        public override StatusEffectConfig MakeConfig()
        {
            StatusEffectConfig config = GetDefaultStatusEffectConfig();

            config.Type = StatusEffectType.Positive;
            config.HasLevel = false;
            config.DurationStackType = StackType.Add;
            config.DurationDecreaseTiming = DurationDecreaseTiming.TurnStart;
            config.HasDuration = true;

            config.HasCount = false;

            return config;
        }
    }

    /// <summary>
    /// 反脆弱
    /// </summary>
    [EntityLogic(typeof(ProbeReversalFragilSeDef))]
    public sealed class ProbeReversalFragilSe : StatusEffect
    {
        [UsedImplicitly]
        public int Value
        {
            get
            {
                GameRunController gameRun = base.GameRun;
                if (gameRun == null || !(base.Owner is EnemyUnit))
                {
                    return 30;
                }
                return Math.Min(30 + gameRun.FragilExtraPercentage, 100);
            }
        }

        protected override void OnAdded(Unit unit)
        {
            HandleOwnerEvent(unit.BlockShieldGaining, OnBlockGaining);
        }

        private void OnBlockGaining(BlockShieldEventArgs args)
        {
            if (args.Cause == ActionCause.Card || args.Cause == ActionCause.OnlyCalculate)
            {
                float num = 1f + (float)Value / 100f;
                if (args.Type != BlockShieldType.Direct)
                {
                    args.Block *= num;
                    args.Shield *= num;
                }
            }
        }

    }
    #endregion

    #region 反易伤未测试
    public sealed class ProbeReversalVulnerableSeDef : ProbeStatusEffectTemplate
    {
        public override StatusEffectConfig MakeConfig()
        {
            StatusEffectConfig config = GetDefaultStatusEffectConfig();

            config.Type = StatusEffectType.Positive;
            config.HasLevel = false;
            config.DurationStackType = StackType.Add;
            config.DurationDecreaseTiming = DurationDecreaseTiming.TurnStart;
            config.HasDuration = true;

            config.HasCount = false;

            return config;
        }
    }

    /// <summary>
    /// 反易伤
    /// </summary>
    [EntityLogic(typeof(ProbeReversalVulnerableSeDef))]
    public sealed class ProbeReversalVulnerableSe : StatusEffect
    {
        [UsedImplicitly]
        public int Value
        {
            get
            {
                GameRunController gameRun = base.GameRun;
                if (gameRun == null)
                {
                    return 50;
                }
                return 50 + ((base.Owner is PlayerUnit) ? gameRun.PlayerVulnerableExtraPercentage : gameRun.EnemyVulnerableExtraPercentage);
            }
        }

        protected override void OnAdded(Unit unit)
        {
            HandleOwnerEvent(unit.DamageReceiving, OnDamageReceiving);
        }

        private void OnDamageReceiving(DamageEventArgs args)
        {
            DamageInfo damageInfo = args.DamageInfo;
            if (damageInfo.DamageType == DamageType.Attack)
            {
                damageInfo.Damage = damageInfo.Amount * (1f - (float)Value / 100f);
                args.DamageInfo = damageInfo;
                args.AddModifier(this);
            }
        }
    }
    #endregion

    #region 反虚弱未测试
    public sealed class ProbeReversalWeakSeDef : ProbeStatusEffectTemplate
    {
        public override StatusEffectConfig MakeConfig()
        {
            StatusEffectConfig config = GetDefaultStatusEffectConfig();

            config.Type = StatusEffectType.Positive;
            config.HasLevel = false;
            config.DurationStackType = StackType.Add;
            config.DurationDecreaseTiming = DurationDecreaseTiming.TurnStart;
            config.HasDuration = true;

            config.HasCount = false;

            return config;
        }
    }

    /// <summary>
    /// 反虚弱
    /// </summary>
    [EntityLogic(typeof(ProbeReversalWeakSeDef))]
    public sealed class ProbeReversalWeakSe : StatusEffect
    {
        [UsedImplicitly]
        public int Value
        {
            get
            {
                GameRunController gameRun = base.GameRun;
                if (gameRun == null || !(base.Owner is EnemyUnit))
                {
                    return 30;
                }
                return Math.Min(30 + gameRun.WeakExtraPercentage, 100);
            }
        }

        protected override void OnAdded(Unit unit)
        {
            HandleOwnerEvent(unit.DamageDealing, OnDamageDealing);
        }

        private void OnDamageDealing(DamageDealingEventArgs args)
        {
            DamageInfo damageInfo = args.DamageInfo;
            if (damageInfo.DamageType == DamageType.Attack)
            {
                damageInfo.Damage = damageInfo.Amount * (1f + (float)Value / 100f);
                args.DamageInfo = damageInfo;
                args.AddModifier(this);
            }
        }
    }

    #endregion

    #region 天衣大洞
    public sealed class ProbeReversalInvincibleEternalSeDef : ProbeStatusEffectTemplate
    {
        public override StatusEffectConfig MakeConfig()
        {
            StatusEffectConfig config = GetDefaultStatusEffectConfig();

            config.Type = StatusEffectType.Negative;
            config.HasLevel = false;
            config.DurationStackType = StackType.Add;
            config.DurationDecreaseTiming = DurationDecreaseTiming.TurnStart;
            config.HasDuration = true;

            config.HasCount = false;

            return config;
        }
    }

    /// <summary>
    /// 天衣大洞
    /// </summary>
    [EntityLogic(typeof(ProbeReversalInvincibleEternalSeDef))]
    public sealed class ProbeReversalInvincibleEternalSe : StatusEffect
    {
        public override string UnitEffectName => "InvincibleLoop";

        protected override void OnAdded(Unit unit)
        {
            HandleOwnerEvent(unit.HealingReceiving, OnHealingReceiving);
        }

        private void OnHealingReceiving(HealEventArgs args)
        {
            int num = args.Amount.RoundToInt();
            if (num > 1)
            {
                NotifyActivating();
                args.Amount = 1;
                args.AddModifier(this);
            }
        }
    }

    #endregion

    #region 你可怜的道具背叛了你！

    public sealed class ProbeReversalLimitedDamageSeDef : ProbeStatusEffectTemplate
    {
        public override StatusEffectConfig MakeConfig()
        {
            StatusEffectConfig config = GetDefaultStatusEffectConfig();

            config.Type = StatusEffectType.Special;
            config.HasLevel = false;
            config.HasDuration = false;
            config.HasCount = true;

            return config;
        }
    }

    /// <summary>
    /// 你可怜的道具背叛了你！
    /// </summary>
    /// 
    [EntityLogic(typeof(ProbeReversalLimitedDamageSeDef))]
    public sealed class ProbeReversalLimitedDamageSe : StatusEffect
    {
        private int _internalCount;

        protected override void OnAdded(Unit unit)
        {
            base.Highlight = true;
            Count = 200;
            ReactOwnerEvent(unit.TurnStarting, OnTurnStarting);
        }

        private IEnumerable<BattleAction> OnTurnStarting(UnitEventArgs args)
        {
            if (base.Owner != null && !base.Battle.BattleShouldEnd)
            {
                NotifyActivating();
                yield return DamageAction.Reaction(base.Owner, base.Count);
            }
        }
    }

    #endregion
}